package com.tc.repository;

import com.tc.entity.UserInfo;
import com.tc.filter.FilterChain;
import com.tc.filter.IdFilter;
import com.tc.filter.LoginNameFilter;
import com.tc.utils.DataMask;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Repository;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by cai.tian on 2017/5/8.
 */
@Repository
public class MockUserInfoRepository  implements InitializingBean {
    private ConcurrentHashMap<Long,UserInfo> map = new ConcurrentHashMap<Long,UserInfo>();


    public  List<UserInfo> select(UserInfo userInfo){
        FilterChain f=new FilterChain();
        List<UserInfo> userInfos = selectAll();
        f.setUserInfos(userInfos);
        if(userInfo==null) return userInfos;
        if(userInfo.getLoginname()!=null){
            LoginNameFilter lf=new LoginNameFilter();
            lf.setLoginName(userInfo.getLoginname());
            f.addFilter(lf);
        }else if(userInfo.getId()!=0){
            IdFilter id=new IdFilter();
            id.setId(userInfo.getId());
            f.addFilter(id);
        }
        f.doFilter();
        return f.getUserInfos();
    }

    public void insert(UserInfo userInfo){
        map.put(userInfo.getId(),userInfo);
    }

    public void delete(Long id){
        map.remove(id);
    }

    public void update(UserInfo userInfo){
        map.put(userInfo.getId(),userInfo);
    }
    public List<UserInfo> selectAll() {
        List<UserInfo> list=new ArrayList<>();
        for (Map.Entry<Long, UserInfo> entry : map.entrySet()) {
            list.add(entry.getValue().clone());
        }
        return list;
    }
    public List<UserInfo> sort(String filed,List<UserInfo> userInfos){
        List <UserInfo> list=userInfos;
        if(filed.equals("loginname")){
            list.sort(new Comparator<UserInfo>() {
                @Override
                public int compare(UserInfo o1, UserInfo o2) {
                    return o1.getLoginname().compareTo(o2.getLoginname());
                }
            });
        }else if(filed.equals("id")){
            list.sort(new Comparator<UserInfo>() {
                @Override
                public int compare(UserInfo o1, UserInfo o2) {
                    return(int) (o1.getId()-o2.getId());
                }
            });
        }
        return list;
    }
    
    @Override
    public void afterPropertiesSet() throws Exception {
        UserInfo u1=new UserInfo(1L, "andy",DataMask.encode("123"),"田才");
        UserInfo u2=new UserInfo(2L,"carl",DataMask.encode("321"),"Carl");
        UserInfo u3=new UserInfo(3L,"bruce",DataMask.encode("1234"),"Bruce");
        UserInfo u4=new UserInfo(4L,"dolly",DataMask.encode("4321"),"Dolly");
        map.put(u1.getId(),u1);
        map.put(u2.getId(),u2);
        map.put(u3.getId(),u3);
        map.put(u4.getId(),u4);
    }

    public ConcurrentHashMap<Long, UserInfo> getMap() {
        return map;
    }

    public void setMap(ConcurrentHashMap<Long, UserInfo> map) {
        this.map = map;
    }
}
